﻿#ifndef NGX_HTTP_SECURITY_FILTER_H
#define NGX_HTTP_SECURITY_FILTER_H

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "redis_model.h"


#define URI_MAX_LEN 512
#define  MAX_REDIS_COUNT                    15

#define  DEFAULT_HEADER_TOKEN_KEY         "huiduflag"
#define  DEFAULT_HEADER_TOKEN_VALUE       "yes"
#define  DEFAULT_MODEL_SWITCH         		1
#define  DEFAULT_NO_REJECT_SWITCH         	1
#define  DEFAULT_REJECT_TYPE         		1   //1:返回固定页面  0：返回自己

#define  MAX_URL_LEN						512

#define  NGX_SECURITY_PASS                  0
#define  NGX_SECURITY_REJECT               	1
#define  NGX_SECURITY_DECLINED           	2
#define  CHECK_RESULT(result) \
    if(result == NGX_SECURITY_PASS)\
        goto  NGX_SE_PASS;                \
    else if(result == NGX_SECURITY_REJECT)\
        goto   NGX_SE_REJECT;


typedef struct {
    u_char              color;
    u_char              len;
    u_short             conn;
    u_char              data[1];
} ngx_http_huidu_node_t;


typedef struct {
    ngx_shm_zone_t     *shm_zone;
    ngx_rbtree_node_t  *node;
} ngx_http_huidu_cleanup_t;


typedef struct {
    ngx_rbtree_t       *rbtree;
    ngx_int_t           index;
    ngx_str_t           var;
} ngx_http_huidu_ctx_t;


typedef struct {
    ngx_shm_zone_t     *shm_zone;
    ngx_uint_t          conn;
} ngx_http_huidu_limit_t;


typedef struct {
    ngx_str_t       host;               //ip or host
    ngx_uint_t      port;               //port
} ngx_http_huidu_redis_address_t;


typedef struct {
    ngx_http_huidu_redis_address_t *redis_addr;
    ngx_uint_t 			redis_count;
    ngx_str_t          	header_key;
    ngx_str_t			header_value;
    ngx_uint_t			model_switch;				//0：off   1：open  	是否开启模块功能
    ngx_uint_t          log_level;
    ngx_uint_t          status_code;
} ngx_http_huidu_conf_t;


typedef struct {
     ngx_str_t   * data;
     ngx_uint_t      cnt;
}huidu_str_array_t;


typedef struct{
	ngx_str_t 	ip;
	ngx_uint_t 	birthday;
	ngx_uint_t 	expire;
}tmp_black_ip_t;

typedef struct {
	tmp_black_ip_t   * data;
    ngx_uint_t      cnt;
}tmp_black_ip_array_t;

typedef struct {
	huidu_str_array_t    ip_list;
	huidu_str_array_t    user_id_list;
	huidu_str_array_t    appid_list;
	huidu_str_array_t    bbversion_list;
} huidu_limit_info_t;


void printImInfo(ngx_http_request_t *r);

void
huidu_request_handle(ngx_http_request_t *r,
		ngx_http_huidu_conf_t    *scf,
		huidu_limit_info_t  *huidu_config);


#endif
